package com.zyz.O_网络编程.UDP.demo2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author: vick
 * @Date: 2025/4/17
 * @Description:
 * @version: 1.0
 */
public class Receive {

    public static void main(String[] args) throws Exception {
        //建立接收方的DatagramSocket
        DatagramSocket datagramSocket = new DatagramSocket(8888);

        //一直监听获取数据
        while (true) {
            //建立数据包 DatagramPacket
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);

            // 调用方法 ，接收消息
            datagramSocket.receive(datagramPacket);
            String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println(str + ":" + datagramPacket.getAddress());
        }
    }
}
